<layout name="layout" />
<script type="text/javascript" src="/Js/dist/daterangepicker.js"></script>
<script type="text/javascript" src="/Js/dist/date.js"></script>
<style type="text/css">
    #campaign_relation {
        width: 100%;
        margin: 5px auto;
        text-align: center;
        font-family: "微软雅黑", "Microsoft YaHei", "黑体", Arial, Helvetica, sans-serif;
        font-weight: normal;
    }

    #campaign_relation span {
        font-size: 16px;
        padding: 10px 0;
    }

    #campaign_relation ul {
        list-style-type: none;
        text-align:left !important;
        overflow: hidden;
        width: 91%;
        margin-left:10px;
        margin-top: 5px;

    }

    #campaign_relation ul li {
        float: left;
        width: 24%;
        padding: 5px 0;
    }
    #dataList {
        width: 100%;
        margin: 0px auto;
        border-collapse: collapse;
        text-align: center;
        font-family: "微软雅黑", "Microsoft YaHei", "黑体", Arial, Helvetica, sans-serif;
        font-weight: normal;
    }

    #dataList caption {
        padding: 10px 0;
        color: #333;
        font-size: 20px;
    }

    #dataList thead th {
        border-bottom: 1px solid #D8D8D8;
        background: #EEEEEE;
    }

    #dataList th,
    td {
        padding: 10px 0;
        border: 0;
        font-weight: normal;
    }

    #dataList td {
        background: transparent;
        border-bottom: 1px solid #D8D8D8;
    }
    .plr10{
        padding-left: 10px;
        padding-right: 10px;
    }

    .word {
        padding-left: 10px;
        padding-right: 10px;
    }

    .num_iid,
    .keyword_id,
    .adgroup_id,
    .campaign_id,
    .tactics_id {
        display: none;
    }

    .rank,
    .impression {
        background: url(/Img/loader2.gif) center center no-repeat !important;
    }

    a.btn:hover {
        text-decoration: none;
        color: #333;
        border: none;
    }

    .ctr {
        text-align: center;
    }

    .ctr span {
        display: block;
        margin-left: 35%;
    }

    .cpc {
        text-align: center;
    }

    .cpc span {
        display: block;
        margin-left: 25%;
    }

</style>
<script type="text/javascript">

    var user_id = "{$me['user_id']}";
    //按日期显示数据
    $(function(){
        var _datepicker = new wt.datepicker("#table_datepicker" , {
            startDate:  Date.today().add({ days: -1 }).toString('yyyy-MM-dd'),
            onChange : function(start, end){
                //得关键词报表
                getWordReport(start,end);
            }
        });
    });

    $(function(){
        //全选按钮
        $('#selectAll').click(function(){
            $(this).toggleClass('btn_unselectall');
            if(/\bbtn_unselectall\b/.test($(this).attr('class'))){
                $('input').removeAttr('checked');
            }else{
                $('input').attr('checked','checked');
            }
        });

        //恢复原价
        var post_sign = true;

        $('tr[data-keyword_id]').on('click', 'input.select_items', function(event){
            var $tr = $(event.delegateTarget), $this = $(this);
            if($this.is(':checked')){
                var word = $tr.find('.word').html(),
                        original_value = $tr.find('.original_value').html(),
                        current_value = $tr.find('.max_price').html();
                if (original_value == current_value) {
                    wt.showBox(word+'的现在价格和原始价格相等', '温馨提示', {cancel: false});
                    $this.attr('checked', false);
                }
            }
        });

        $('#originalValue').click(function(){
            var $input = $("input");
            wt.showBox('确认恢复原价!', '温馨提示', function(res){
                if(res){
                    $(this).attr('disabled','disabled');
                    var keyword_ids_data = [];
                    $input.each(function(){
                        var $this = $(this);
                        if($this.is(':checked')) {
                            var keyword_id = $this.data('keyword_id');
                            if(keyword_id){
                                var $tr = $('tr[data-keyword_id=' + keyword_id + ']'),
                                        word = $tr.find('.word').text(),
                                        original_value = $tr.find('.original_value').text(),
                                        current_value = $tr.find('.max_price').text();
                                if (original_value != current_value) {
                                    keyword_ids_data.push(keyword_id);
                                }
                            }
                        }
                    });

                    if(keyword_ids_data.length == 0){
                        wt.showBox('请选择关键词!', '温馨提示', {cancel: false});
                        return;
                    }

                    $.post('./restoreOriginalValue',{
                        tactics_id: $('.tactics_id').html(),
                        keyword_ids: keyword_ids_data
                    },function(res,status){
                        var dataObj = $.encodeJSON(res);

                        //失败
                        if (dataObj.data.code == 0) {
                            wt.showBox(dataObj.data.msg, '操作失败', {cancel: false});
                        }else if(dataObj.data.code == 1){
                            //成功
                            //异步刷新当前出价
                            var srcElement=[];
                            $input.each(function(index,element){
                                if ($(this).is(':checked')) {
                                    srcElement.push($(this).parent().siblings('.max_price'));
                                    $(this).parent().siblings('.max_price').html(' ');
                                    $(this).parent().siblings('.max_price').addClass('rank');
                                }
                            });

                            //发送数据
                            if (keyword_ids_data.length>0) {
                                $.post('./AjaxGetCurrentValue',{
                                    keyword_ids: keyword_ids_data
                                },function(currentValue_data,currentValue_status){
                                    var currentValueObj = eval("("+currentValue_data+")");
                                    $.each(currentValueObj,function(index,element){
                                        srcElement[index].html('￥'+element.max_price/100);
                                        srcElement[index].removeClass('rank');
                                    });
                                });
                            }
                            $('#originalValue').removeAttr('disabled');
                            wt.showBox('恢复原价成功!', '提示', {cancel: false});
                        }
                    });
                }
            });
        });

    });

    //得关键词当前排名
    $(function(){

        var fillupCurrentRank = function($tbody, user_id, device, ids){
            var data = {},
                    url = 'algorithmservice/users/' + user_id + '/realtimeRanks';
            if(device == 'mobile'){
                data.device = 2;
            }else{
                data.device = 1;
            }
            data.keyword_ids = ids.join(',');
            wt.api.getJSON(url, data, function(res){
                if(res.data && res.data.length > 0){
                    $.each(res.data, function(){
                        var convert = (device == 'mobile')?wt.mappings.mobileRank:wt.mappings.pcRank;
                        var $tr = $tbody.find('tr[data-keyword_id="'+ this.keyword_id + '"]');
                        $tr.find('.rank').removeClass('rank').html(convert(this.current_rank));
                    });
                }
            });
        };

        var fillupWantRank = function($tbody, device){
            var convert = (device == 'mobile')?wt.mappings.mobileRank:wt.mappings.pcRank;
            $tbody.find('tr .want_rank').each(function(){
                var $this = $(this);
                $this.html(convert($this.data('want_rank')));
            });
        };

        var kids_data = [], ids = [];
        $('.word').each(function(index,element){
            var keyword_id = $('.keyword_id').eq(index).html();
            kids_data.push( {'keyword_id' : keyword_id, 'word' : $('.word').eq(index).html(), itemId : $('.num_iid').eq(index).html()});
            ids.push(keyword_id);
        });
        //异步获取当前排名
        if (kids_data.length) {
            fillupCurrentRank($('tbody'), user_id, 'pc', ids);
        }
        fillupWantRank($('tbody'), 'pc');
    });

    //得关键词报表
    $(function(){

        var date =new Date();
        var date_start = date.getFullYear()+'-'+(date.getMonth()+1)+'-'+(date.getDate()-1),   //默认显示昨天的数据
                date_end =  date.getFullYear()+'-'+(date.getMonth()+1)+'-'+(date.getDate()-1);

        getWordReport(date_start,date_end);
    });

    //得关键词报表
    //start  开始日期
    //end    结束日期
    function getWordReport (start,end) {

        //添加等待动画
        $('.impression').addClass('impression');

        //取得要发送的数据
        var postData = [];
        $('.adgroup_id').each(function(index,element){
            postData.push({
                'adgroup_id': $('.adgroup_id').eq(index).html(),
                'campaign_id': $('.campaign_id').eq(index).html(),
                'keyword_id': $('.keyword_id').eq(index).html(),
            });
        });

        //发送数据
        if (postData.length) {
            $.post('./getRankWordReport',{
                adgroupCampaign_id: postData,
                date_start: start,
                date_end: end
            },function(data,status){

                //回写数据
                // alert(data);
                var dataObj = eval('('+data+')');
                if (dataObj != '') {
                    $.each(dataObj,function(index,dataElement){
                        $('#keyword_id_impress'+index).html(dataElement.impression);                    //展现量
                        $('#keyword_id_click'+index).html(dataElement.click);                           //点击量
                        $('#keyword_id_ctr'+index).html(dataElement.ctr+'%');                           //点击率
                        $('#keyword_id_cpc'+index).html('￥'+dataElement.cpc);                           //每点击花费
                        $('#keyword_id_cost'+index).html('￥'+dataElement.cost);                        //总花费
                        $('#keyword_id_favtotal'+index).html(dataElement.favtotal);                     //总收藏数
                        $('#keyword_id_carttotal'+index).html(dataElement.carttotal);                   //购物车总数
                        $('#keyword_id_roi'+index).html(dataElement.roi);                                          //roi
                        $('#keyword_id_transactiontotal'+index).html('￥'+dataElement.transactiontotal/100);                   //总成交金额
                        $('#keyword_id_transactionshippingtotal'+index).html(dataElement.transactionshippingtotal);   //总成交笔数
                    });
                }else{
                    //如果没有数据
                    $('.keyword_id').each(function(index,element){
                        var keyword_id = $('.keyword_id').eq(index).html();
                        $('#keyword_id_impress'+keyword_id).html(0);
                        $('#keyword_id_click'+keyword_id).html(0);
                        $('#keyword_id_ctr'+keyword_id).html('0%');
                        $('#keyword_id_cpc'+keyword_id).html('￥0');
                        $('#keyword_id_cost'+keyword_id).html('￥0');
                        $('#keyword_id_roi'+keyword_id).html('0');
                        $('#keyword_id_transactiontotal'+keyword_id).html('￥0');
                        $('#keyword_id_transactionshippingtotal'+keyword_id).html(0);
                        $('#keyword_id_favtotal'+keyword_id).html(0);
                        $('#keyword_id_carttotal'+keyword_id).html(0);
                    });
                }


                //去掉等待动画
                $('.impression').removeClass('impression');
            });
        }
    }
</script>
<!--<ul class="breadcrumbs breadcrumbs2" style="margin-top:39px;">
    <li><a href="/">首页</a><span class="divider">&#187;</span><a href="/">智能卡位</a><span class="divider">&#187;</span><a href="/ranking/show">我的卡位策略</a><span class="divider">&#187;</span><a style="color:#333;">详情</a></li>
</ul>-->
<div class="pageheader notab">
    <h1 class="pagetitle">
        <a href="/" class="linkWay">首页/智能卡位/我的卡位策略</a>
        &nbsp;<span class="divider">»</span>&nbsp;
        <a href="/ranking/show" class="linkWay">我的卡位策略</a>
        &nbsp;<span class="divider">»</span>&nbsp;
        <a href="#" class="currentWay">详情</a>
    </h1>
</div>
<div id="campaign_relation" style="/*margin-top:60px;*/overflow:hidden;">
    <span style="display:inline-block;text-align:left;width:8%;float:left;">卡位详情:</span>
    <ul style="display:;text-align:center;border:1px solid #ccc;float:left;">
        <li style="border-right:1px solid #ccc;text-align:center;">名称:&nbsp;{$res[0]['tactics_name']}</li>
        <li style="border-right:1px solid #ccc;text-align:center;">状态:&nbsp;
            <php>
                switch ( $res[0]['tactics_status'] ) { case 1 : echo "开启" ; break; case 2 : echo "暂停" ; break; case 3 : echo "完成" ; break; }
            </php>
        </li>
        <li style="border-right:1px solid #ccc;text-align:center;">卡位轮次:&nbsp;{$res[0]['executions']}</li>
        <li style="text-align:center;">上次卡位时间:&nbsp;
            <php>echo date("Y-m-d H:i:s",$res[0]['tactics_modified_time']);</php>
        </li>
    </ul>
</div>
<div>
    <div style="text-align:left !important;float:left;">
        <button id="originalValue" class="stdbtn add-con" style="background:#229fdd;color:#fff;">恢复原价</button>
    </div>
    <div style="float:right;" id="table_datepicker"></div>
    <table id="dataList">
        <!--<caption>卡位详情</caption>-->
        <thead>
        <tr>
            <th width="24px"><a id="selectAll" class="btn btn3 btn_unselectall btn_selectall tooltips" href="javascript:void(0);" title="全选">全选</a></th>
            <th width="50px">ID</th>
            <th width="80px">计划</th>
            <th width="100px">推广组</th>
            <th width="100px">关键词</th>
            <th width="100px">卡位信息</th>
            <th width="100px">信息时间</th>
            <th width="auto">原始出价</th>
            <th width="auto">当前出价</th>
            <th width="auto">当前排名</th>
            <th width="auto">期望排名</th>
            <th width="auto">展现量</th>
            <th width="auto">点击量</th>
            <th width="auto">点击率</th>
            <th width="auto">ppc</th>
            <th width="auto">总花费</th>
            <th width="auto">roi</th>
            <th width="auto">总成交金额</th>
            <th width="auto">总成交笔数</th>
            <th width="auto">收藏数</th>
            <th width="auto">购物车数</th>
        </tr>
        </thead>
        <tbody id="html">
        <php>
            foreach ( $res as $key => $msg ) {
        </php>
        <tr data-keyword_id="{$msg['keyword_id']}">
            <td class="num_iid">{$msg['num_iid']}</td>
            <td class="keyword_id">{$msg['keyword_id']}</td>
            <td class="adgroup_id">{$msg['adgroup_id']}</td>
            <td class="campaign_id">{$msg['campaign_id']}</td>
            <td class="tactics_id">{$msg['tactics_id']}</td>
            <!-- <td class="original_value">{$msg['original_value']}</td> -->
            <td><input type="checkbox" style="margin-left:3.85px;" class="select_items" data-campaign_id="2947451" data-keyword_id="{$msg['keyword_id']}" id="keyword_id{$msg['keyword_id']}" /></td>
            <td>{$key+1}</td>
            <td>
                <a href="/campaign/manage?campaign_id={$msg['campaign_id']}">{$msg['campaigns_title']}</a>
            </td>
            <td class="plr10">
                <a href="/adgroup/manage?adgroup_id={$msg['adgroup_id']}">{$msg['adgroups_title']}</a>
            </td>
            <td class="word">{$msg['word']}</td>
            <td>
                <php>if(!$msg['msg']){echo '没有附加信息';}else{echo $msg['msg'];}</php>
            </td>
            <td>{$msg['update_time_keyword']|display_date}</td>
            <td class="original_value"><php>if(!$msg['original_value']){echo '没有设置原始出价';}else{echo '￥'.$msg['original_value']/100;}</php></td>
            <td class="max_price">
                <if condition="$msg['device'] == 1">
                    ￥{$msg['max_mobile_price']/100}<else/>￥{$msg['max_price']/100}
                </if>
            </td>
            <td class="rank"></td>
            <td class="want_rank" data-want_rank="{$msg['want_rank']}"></td>
            <td class="impression" id="keyword_id_impress{$msg['keyword_id']}"></td>
            <td class="impression" id="keyword_id_click{$msg['keyword_id']}"></td>
            <td class="impression ctr" id="keyword_id_ctr{$msg['keyword_id']}"></td>
            <td class="impression cpc" id="keyword_id_cpc{$msg['keyword_id']}"></td>
            <td class="impression" id="keyword_id_cost{$msg['keyword_id']}"></td>
            <td class="impression" id="keyword_id_roi{$msg['keyword_id']}"></td>
            <td class="impression" id="keyword_id_transactiontotal{$msg['keyword_id']}"></td>
            <td class="impression" id="keyword_id_transactionshippingtotal{$msg['keyword_id']}"></td>
            <td class="impression" id="keyword_id_favtotal{$msg['keyword_id']}"></td>
            <td class="impression" id="keyword_id_carttotal{$msg['keyword_id']}"></td>
        </tr>
        <php>
            }
        </php>
        </tbody>
    </table>
</div>